﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services.Description;

namespace Cake_Management.Controllers
{
    public class HT_AddressController : Controller
    {
        // GET: HT_Address
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(Address address)
        {
            new BLL.B_Address().Add(address);
            return Json(new { Code = 200, Message = "添加成功" });
        }


        public ActionResult GetAll()
        {
            var result = new BLL.B_Address().GetAll();
            var sj = from a in result
                     select new
                     {
                         a.Id,
                         a.ToName,
                         a.Phone,
                         a.Areas,
                         a.AddressInfo,
                         IsDefault = a.IsDefault.Value ? "已启用" : "禁用",
                         a.CustomerId,
                         a.SendName,
                         a.SendPhone,
                         Operation = $"<div data-id='{a.Id}' class='btn btn-warning data-edit' onclick='Edit(this)'>编辑</div>      <div data-id='{a.Id}' class='btn btn-danger data-del'onclick='Del(this)'>删除</div>"

                     };
            var data = new { data = sj };
            return Json(data, JsonRequestBehavior.AllowGet);
        }




        // GET: HT_Address/Edit/5
        [HttpGet]
        public ActionResult Edit(int id)
        {
            var data = new BLL.B_Address().FindById(id);
            return PartialView(data);
        }

        // POST: HT_Address/Edit/5
        [HttpPost]
        public ActionResult Edit(Address address)
        {
            try
            {
                // TODO: Add update logic here
                new BLL.B_Address().Updata(address.Id, address);
                return Json(new { Code = 200, Message = "更改成功" });
            }
            catch(Exception ex)
            {
                return Json(new { code = 500,Message = ex.Message });
            }
        }

        // GET: HT_Address/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add update logic here
                new BLL.B_Address().Delete(id);
                return Json(new { Code = 200, Message = "更改成功" });
            }
            catch (Exception ex)
            {
                return Json(new { code = 500, Message = ex.Message });
            }
        }

        
        
    }
}
